Entity Framework এ, Entity Relationships এবং Navigational Properties ডেটাবেসের টেবিলগুলোর মধ্যে সম্পর্ক এবং টেবিলগুলির মধ্যে কিভাবে ডেটা সম্পর্কিত থাকবে তা কনফিগার করে। Entity Relationships হল এক বা একাধিক টেবিলের মধ্যে সম্পর্ক গঠন করার পদ্ধতি, এবং Navigational Properties হল এমন প্রপার্টি যা Entity গুলির মধ্যে সম্পর্কের মাধ্যমে ডেটা অ্যাক্সেস করার সুবিধা প্রদান করে।
Entity Framework এ বিভিন্ন ধরনের সম্পর্ক থাকতে পারে, যেমন:
একটি One-to-One সম্পর্ক হয় যখন দুটি Entity এর মধ্যে একে অপরের সাথে একটিই সম্পর্ক থাকে। উদাহরণস্বরূপ, Student এবং StudentProfile Entity এর মধ্যে এক-থেকে-এক সম্পর্ক থাকতে পারে।
public class Student
{
public int StudentId { get; set; }
public string Name { get; set; }
public StudentProfile StudentProfile { get; set; }
}
public class StudentProfile
{
public int StudentProfileId { get; set; }
public string Bio { get; set; }
public int StudentId { get; set; }
public Student Student { get; set; }
}
এখানে, Student এবং StudentProfile Entity গুলির মধ্যে এক-থেকে-এক সম্পর্ক আছে। StudentProfile Entity তে StudentId ফরেন কি হিসেবে কাজ করছে।
এটি হলো সবচেয়ে সাধারণ সম্পর্ক, যেখানে একটি Entity এর একটি রেকর্ড একাধিক রেকর্ডের সাথে সম্পর্কিত থাকে। যেমন, একটি Course Entity একাধিক Student Entity এর সাথে সম্পর্কিত থাকতে পারে।
public class Course
{
public int CourseId { get; set; }
public string CourseName { get; set; }
public ICollection<Student> Students { get; set; }
}
public class Student
{
public int StudentId { get; set; }
public string Name { get; set; }
public int CourseId { get; set; }
public Course Course { get; set; }
}
এখানে, Course Entity তে ICollection Students ব্যবহার করা হয়েছে, যা নির্দেশ করে যে একটি Course একাধিক Student এর সাথে সম্পর্কিত। আর Student Entity তে Course এর সাথে সম্পর্ক রয়েছে।
Many-to-Many সম্পর্ক তখন তৈরি হয় যখন দুটি Entity একে অপরের সাথে একাধিক সম্পর্ক রাখে। যেমন, একটি Student একাধিক Course এর সাথে সম্পর্কিত হতে পারে এবং একটি Course একাধিক Student এর সাথে সম্পর্কিত হতে পারে।
public class Student
{
public int StudentId { get; set; }
public string Name { get; set; }
public ICollection<Course> Courses { get; set; }
}
public class Course
{
public int CourseId { get; set; }
public string CourseName { get; set; }
public ICollection<Student> Students { get; set; }
}
এখানে, Student এবং Course Entity গুলির মধ্যে ICollection এবং ICollection এর মাধ্যমে Many-to-Many সম্পর্ক প্রতিষ্ঠিত হয়েছে। EF Core সাধারণত Join Table তৈরি করে এই সম্পর্ক সঠিকভাবে রেপ্রেজেন্ট করতে।
Navigational Properties হল Entity ক্লাসের প্রপার্টি, যা অন্যান্য Entity গুলির মধ্যে সম্পর্কের মাধ্যমে ডেটা অ্যাক্সেস করতে সাহায্য করে। Navigational properties সাধারণত দুটি Entity এর মধ্যে সম্পর্ক স্থাপন এবং তাদের মধ্যে সম্পর্কিত ডেটা পাঠানোর কাজ করে।
নির্দিষ্ট Entity তে সম্পর্কিত অন্য Entity এর ডেটা অ্যাক্সেস করতে Navigational Properties ব্যবহার করা হয়।
যেমন, আমরা যদি একটি Course এর সাথে সম্পর্কিত সব Student গুলো দেখতে চাই, তাহলে Course Entity তে Students নামক Navigational Property ব্যবহার করা হবে।
public class Course
{
public int CourseId { get; set; }
public string CourseName { get; set; }
public ICollection<Student> Students { get; set; }
}
এখানে, Students
একটি Navigational Property যা Course Entity এর সাথে সম্পর্কিত Student গুলোকে রেফারেন্স করে। এর মাধ্যমে আপনি একটি কোর্সের সব ছাত্রদের অ্যাক্সেস করতে পারবেন।
Many-to-Many সম্পর্কের জন্য Navigational Properties ব্যবহার করা হয় দুইটি Entity তে। যেমন, Student এবং Course এর মধ্যে একটি Many-to-Many সম্পর্ক ছিল। এখানে, Student এবং Course উভয় Entity তে Navigational Properties থাকবে।
public class Student
{
public int StudentId { get; set; }
public string Name { get; set; }
public ICollection<Course> Courses { get; set; }
}
public class Course
{
public int CourseId { get; set; }
public string CourseName { get; set; }
public ICollection<Student> Students { get; set; }
}
এখানে, Student Entity তে Courses
এবং Course Entity তে Students
Navigational Properties ব্যবহার করা হয়েছে।
Microsoft.EntityFrameworkCore.Proxies
প্যাকেজ ইনস্টল করতে হয় এবং ডিফল্টভাবে সম্পর্কিত Entity গুলির জন্য Navigational Properties লোড করা হয়।services.AddDbContext<ApplicationDbContext>(options =>
options.UseLazyLoadingProxies().UseSqlServer(connectionString));
var studentsWithCourses = context.Students
.Include(s => s.Courses)
.ToList();
context.Entry(student)
.Collection(s => s.Courses)
.Load();
common.read_more